package com.doeis.minaserver;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import org.apache.log4j.Logger;

import com.doeis.minaserver.handler.IdlChannelHandlerAdapter;
import com.doeis.minaserver.packet.NettyMessageDecoder;
import com.doeis.minaserver.packet.NettyMessageEncoder;

public class ServerShutdown {
	private static String serverIp = "localhost";//服务器IP
	private static Integer serverPort = SystemProperties.getSystemPort();//服务器端口

	public ServerShutdown(){
	}
	
	public static void main(String[] args) {
		// 配置客户端NIO线程组
		EventLoopGroup group = new NioEventLoopGroup();
		try {
		    Bootstrap b = new Bootstrap();
		    b.group(group).channel(NioSocketChannel.class)
			    .option(ChannelOption.TCP_NODELAY, true)
			    .handler(new ChannelInitializer<SocketChannel>() {
				@Override
				public void initChannel(SocketChannel socketChannel)
					throws Exception {
					socketChannel.pipeline().addLast(new NettyMessageDecoder());
					socketChannel.pipeline().addLast(new NettyMessageEncoder());
					socketChannel.pipeline().addLast("IdlChannelHandlerAdapter",
							new IdlChannelHandlerAdapter());
					socketChannel.pipeline().addLast(new NettyClientShutDownAdapter());
				}
			    });

		    // 发起异步连接操作
		    ChannelFuture f = b.connect(serverIp, serverPort).sync();
			// 当代客户端链路关闭
//		    f.channel().closeFuture().sync();
		    
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
		    // 优雅退出，释放NIO线程组
		    group.shutdownGracefully();
		}
	}
	
}


